//
//  XLTabBar.swift
//  WeiBo
//
//  Created by macbook on 16/10/17.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit

//声明协议
protocol XLTabBarDelegate:NSObjectProtocol {
  //声明协议方法
    func composeBtnClickPushController()
    
}

class XLTabBar: UITabBar {

    //声明代理
    weak var XLDelagate:XLTabBarDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置宽度
        let winth = KScreenW / 5
        //记录索引
        var index:CGFloat = 0
        //循环遍历得到子控件
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
               //设置宽度
                value.frame.size.width = winth
                value.frame.origin.x = winth * index
                index++
                
                //把中间位置腾出来
                if index == 2 {
                    index++
                }
                
            }
            
        }
        //设置中间按钮居中
        composeBtn.center.x = self.frame.size.width * 0.5
        composeBtn.center.y = self.frame.size.height * 0.5
        
    }
    
    
    func setupUI()
    {
        addSubview(composeBtn);
    }
    
  //设置按钮
    private lazy var composeBtn:UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal);
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: #selector(XLTabBar.composeBtnClick), forControlEvents: .TouchUpInside)
        
        return btn;
    }();
    
    
   @objc private func composeBtnClick()
    {
        XLDelagate?.composeBtnClickPushController()
        
    }
    
    
}




